<?php

/*
 * Writer - Generate reports Gerador de relatórios em pdf @package CI_Report @category Libraries @author Carlos O Carvalho @license http://www.opensource.org/licenses/MIT @version 1.1.0 Use $this->load->library('reports/writepdf, options array('paper'=>''A4'))
 */
define ( 'FPDF_FONTPATH', APPPATH . 'libraries/reports/fpdf/font/' );

require ('fpdf/fpdf.php');
require ('customfpdf.php');
class WritePDF {
	private $styles = array ();
	private $pdf = null;
	private $colcountter = 0;
	var $widths;
	var $hstyles = null;
	private $filename = 'default_file';
	private $_config = array (
			'orientation' => 'P',
			'paper' => 'A4',
			'marginLeft' => 20,
			'marginRight' => 20,
			'marginBottom' => 20,
			'marginTop' => 20,
			'unit' => 'mm',
			'fontSize' => '12',
			'header' => '',
			'footer' => '',
			'widths' => array (),
			'styles' => array () 
	);
	private $output = 'I';
	private $index;
	private $index_h = - 1;
	private $index_f = - 1;
	private $index_b = - 1;
	private $data = array (
			'body' => array (),
			'header' => array (),
			'footer' => array () 
	);
	private $start_header = false;
	private $start_footer = false;
	function __construct() {
		// armazenando as larguras
	}
	function config($config) {
		$this->_config = array_merge ( $this->_config, $config );
	}
	function add_style($name, $fontface, $fontsize, $fontstyle, $fontcollor, $fillcollor) {
		$this->styles [$name] = array (
				'fontface' => $fontface,
				'fontsize' => $fontsize,
				'fontstyle' => $fontstyle,
				'fontcollor' => $fontcollor,
				'fillcollor' => $fillcollor 
		);
	}
	function start_header() {
		$this->start_header = true;
	}
	function end_header() {
		$this->start_header = false;
	}
	
	/*
	 * aplicando o style @param $namestyle
	 */
	function apply_style($namestyle) {
		if (isset ( $this->styles [$namestyle] )) :
			
			$style = $this->styles [$namestyle];
			// aplicando as configuarcoes
			$this->pdf->SetFont ( $style ['fontface'], $style ['fontstyle'] );
			$this->pdf->SetFontSize ( $style ['fontsize'] );
			// text color
			$color_array = self::convert_to_rgb ( $style ['fontcollor'] );
			$this->pdf->SetTextColor ( $color_array ['r'], $color_array ['g'], $color_array ['b'] );
			$color_array = self::convert_to_rgb ( $style ['fillcollor'] );
			$this->pdf->SetFillColor ( $color_array ['r'], $color_array ['g'], $color_array ['b'] );
		
		
		
		
		
		
		
         endif;
	}
	function add_widths($widths) {
		$this->widths = $widths;
	}
	function add_row() {
		if ($this->start_header) {
			$this->index_h += 1;
			$this->index = $this->index_h;
			
			$this->data ['header'] [$this->index] = array ();
		}
		if ($this->start_footer) {
			$this->index_f += 1;
			$this->index = $this->index_f;
			$this->data ['footer'] [$this->index] = array ();
		}
		
		if (! $this->start_header && ! $this->start_footer) {
			$this->index_b += 1;
			$this->index = $this->index_b;
			$this->data ['body'] [$this->index] = array ();
		}
	}
	function add_cell($data, $align, $style = null, $colspan = 1, $border = 1) {
		$cell = new stdClass ();
		$cell->style = $style;
		$cell->align = $align;
		$cell->colspan = $colspan;
		$cell->border = $border;
		$cell->data = $data;
		if ($this->start_header) {
			
			$cell->style = isset ( $this->styles [$style] ) ? ( object ) array (
					'fontname' => $this->styles [$style] ['fontface'],
					'fontstyle' => $this->styles [$style] ['fontstyle'],
					'fontsize' => $this->styles [$style] ['fontsize'],
					'fontcolor' => self::convert_to_rgb ( $this->styles [$style] ['fontcollor'] ),
					'fillcolor' => self::convert_to_rgb ( $this->styles [$style] ['fillcollor'] ) 
			) : false;
			$this->data ['header'] [$this->index] [] = $cell;
		}
		if ($this->start_footer) {
			$this->data ['footer'] [$this->index] [] = $cell;
		}
		if (! $this->start_header && ! $this->start_footer) {
			$this->data ['body'] [$this->index] [] = $cell;
		}
	}
	function save_file($name = '') {
		$name = ! empty ( $name ) ? $name : $this->filename;
		$this->filename = $name . '.pdf';
		$this->output = 'F';
		$this->_render ();
	}
	function add_header($data) {
		$this->pdf->WriteHTML ( $data );
	}
	function add_footer($data = '') {
		$this->pdf->WriteHTML ( $data );
	}
	function show_file($name = '') {
		$name = ! empty ( $name ) ? $name : $this->filename;
		$this->filename = $name . '.pdf';
		
		$this->_render ();
	}
	function download_file($name = '') {
		$name = ! empty ( $name ) ? $name : $this->filename;
		$this->filename = $name . '.pdf';
		$this->output = 'D';
		$this->_render ();
	}
	function get_file_name() {
		function get_file_extension($file_name) {
			return substr ( strrchr ( $file_name, '.' ), 1 );
		}
	}
	function get_page() {
		return $this->pdf->PageNo ();
	}
	static function convert_to_rgb($color) {
		$r = hexdec ( substr ( $color, 1, 2 ) );
		$g = hexdec ( substr ( $color, 3, 2 ) );
		$b = hexdec ( substr ( $color, 5, 2 ) );
		
		return array (
				'r' => $r,
				'g' => $g,
				'b' => $b 
		);
	}
	
	/*
	 * _Render Renderizar o pdf @access private
	 */
	function _render() {
		if (count ( $this->data ['body'] ) === 0) {
			return 'No data';
		}
		
		if (count ( $this->_config ['widths'] ) > 0) {
			$this->widths = $this->_config ['widths'];
		}
		if (count ( $this->_config ['styles'] ) > 0) {
			$this->hstyles = $this->_config ['styles'];
		}
		
		//setlocale ( LC_ALL, 'POSIX' );
		// create obj
		if (is_null ( $this->pdf )) :
			
			$this->pdf = new CustomFPDF ();
			if (count ( $this->data ['header'] ) > 0) {
				$this->pdf->add_header( $this->data ['header'] );
			}
			
			$this->pdf->widths  = $this->widths;
			$this->pdf->PDF ( $this->_config ['orientation'], 'mm', $this->_config ['paper'] );
			$this->pdf->SetMargins ( 6, 10 );
			$this->pdf->AliasNbPages ();
			//$this->pdf->Open ();
			$this->pdf->AddPage ();
		
		endif;
		
		foreach ( $this->data ['body'] as $cell ) {
			$this->_row ();
			foreach ( $cell as $row ) :
			$this->pdf->SetLineWidth(0.1);
				$this->_cell ( $row->data, $row->align, $row->style, $row->colspan, $row->border );
			endforeach
			;
		}
		
		$this->pdf->Output ( $this->filename, $this->output );
	}
	function _row() {
		$this->pdf->Ln (); // quebra de linha
		$this->colcountter = 0;
	}
	function _cell($data, $align, $style = null, $colspan = 1, $border = 1) {
		// apply style in cell
		$this->apply_style ( $style );
		$fontsize = $this->styles [$style] ['fontsize'];
		$width = 0;
		// calculate width from cells
		for($w = $this->colcountter; $w < $this->colcountter + $colspan; $w ++) {
			$width += $this->widths [$w];
		}
		// show cell with @$data
		
		$this->pdf->Cell ( $width, $fontsize - 4, $data, $border, 0, strtoupper ( substr ( $align, 0, 1 ) ), true );
		$this->colcountter ++;
	}
}

